package org.jeecg.modules.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.constant.RedisConstant;
import org.jeecg.modules.article.entity.Article;
import org.jeecg.modules.article.mapper.ArticleMapper;
import org.jeecg.modules.article.service.IArticleService;
import org.jeecg.modules.article.vo.RotationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Description: 文章
 * @Author: jeecg-boot
 * @Date:   2020-10-14
 * @Version: V1.0
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public int updateReadingNum(String articleId) {
        int readingNum=articleMapper.readingNum(articleId);

        readingNum=readingNum+1;
       int a= articleMapper.updateReadingNum(articleId,readingNum);
        return a;
    }

    @Override
    public Page<Article> queryListByHomePageIsTrue(Page<Article> page) {
        List<Article> articles = articleMapper.queryListByHomePageIsTrue(page);
        // stream流控制排序
        List<Article> list = articles
                .stream()
                .sorted(Comparator
                        .comparing(Article::getCreateTime)
                        .reversed())
                .collect(Collectors.toList());
        return page.setRecords(list);
    }

    @Override
    public List<RotationVO> getTopRotation(int update) {
        // 1表示文章进行了更新操作，提前更新一次
        if (update==1){
            List<RotationVO> topRotation = articleMapper.getTopRotation();
            topRotation.forEach(res->{
                res.setArticleImageString(res.getArticleImageString());
            });
            // 把轮播图缓存到redis中
            try {
                redisUtil.set(RedisConstant.TOPROTATION,topRotation,TimeUnit.HOURS.toSeconds(3));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return topRotation;
        }

        Object obj = redisUtil.get(RedisConstant.TOPROTATION);
        // 查看redis中是否缓存
        if (obj!=null){
            return (List<RotationVO>) obj;
        }
        return getTopRotation(1);
    }


    @Override
    public Page<Article> getSortArticleByColumn(Page<Article> page, String columnName) {
        List<Article> articleList = articleMapper.queryArticleByColumn(page,columnName);
//        List<Article> list = articleList.
//                stream().
//                sorted(Comparator.comparing(Article::getCreateTime).reversed()).
//                sorted(Comparator.comparing(Article::getArticleColumnTop).reversed()).
//                collect(Collectors.toList());
        return page.setRecords(articleList);
    }

    @Override
    public List<Article> myListByIds(List<String> arrayIds) {
        List<Article> list = articleMapper.queryArticleByIds(arrayIds);
        return list;
    }

}
